//
//  mainEGAViewController.m
//  EGA_Archives_iOS
//
//  Created by gad on 1/6/56 BE.
//  Copyright (c) 2556 EGA. All rights reserved.
//
#define ROW_HEIGHT 80
#define CELL_WIDTH 300
#define LABEL_HEIGHT 50
#import "EGAEmployeeInfo.h"
#import "EGAAppDelegate.h"
#import "mainEGAViewController.h"
#import "menuEGAViewController.h"
#import "arcAssignEGAViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "taskListVC.h"
#import "taskListAdmin.h"
#import "EGAMainViewController.h" //yok

@interface mainEGAViewController ()
- (void)showMenu;
- (void)hideMenu;
- (IBAction)handleTap:(UITapGestureRecognizer *)sender;
@end
    
@implementation mainEGAViewController
-(EGAAppDelegate*)appDelegate
{
    return  (EGAAppDelegate*)[[UIApplication sharedApplication] delegate];
}
NSString * role;
@synthesize popoverColor = _popovrColor;
@synthesize summary = _summary;
@synthesize lbTitleUserName;
- (void)viewDidLoad
{
    [super viewDidLoad];
    //  NSLog(@"appDelegate:%@", self.appDelegate.employeeInfo.FirstNameTH);
    NSString *username = [NSString stringWithFormat:@"%@ %@",self.appDelegate.employeeInfo.FirstNameTH,self.appDelegate.employeeInfo.LastNameTH];
    lbTitleUserName.text = username;
        //    _colorController = [[ColorPickerController alloc] initWithNibName:@"ColorPickerController" bundle:nil];
        //
        //    popoverColorView=[[UIView alloc] init];
        //    popoverColor.delegate=(id)self;
        //     popoverColor = [[UIPopoverController alloc] initWithContentViewController:_colorController];
        //    [popoverColor setPopoverContentSize:CGSizeMake(300, 600) animated:NO];
        // Do any additional setup after loading the view, typically from a nib.
    self.menuController = [[menuEGAViewController alloc]initWithNibName:@"menuEGAViewController" bundle:[NSBundle mainBundle]];
    [self.menuController.view setFrame:CGRectMake(0, 0, 0, 1024)];
    [self.menuController setMenuDelegate:self];
    [self.view addSubview:[self.menuController view]];
    [self menuHide];
        //[self showMenu];
    if (UIInterfaceOrientationIsPortrait(self.interfaceOrientation))
    {
        [self addMasterButton];
    }
    [self configureView];
    self.summary = [NSMutableArray array];
    [_summary addObject:@"  งานทั้งหมด"];
    [_summary addObject:@"  ยังไม่ดำเนินการ"];
    [_summary addObject:@"  อยู่ระหว่างดำเนินการ"];
    [_summary addObject:@"  งานที่ดำเนินการเรียบร้อย"];
    
    self.tbSummary.layer.borderColor =[UIColor colorWithRed:0/255.0 green: 100/255.0 blue:0/255.0 alpha:1].CGColor;
    self.tbSummary.layer.borderWidth =  0.5;
    self.tbSummary.layer.cornerRadius=5;
        //    [_summary setDelegate:self];
        //   [_summary setDataSource:self];
        // Do any additional setup after loading the view from its nib.
    

    NSString *RoleName = self.appDelegate.employeeInfo.RoleName;
    role=RoleName;
    if (([role isEqualToString:@"admin"]) || (([role isEqualToString:@"secretary"])) || (([role isEqualToString:@"manager"])))
    {
        EGAMainViewController *_EGAMainViewController = [EGAMainViewController alloc];
        [_contentView addSubview:_EGAMainViewController.view];
    }
    else
    {
        taskListVC *_taskListVC  = [taskListVC alloc];
        _taskListVC.type = @"1";
        [_contentView addSubview:_taskListVC.view];

    }
}



//- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
//{
//    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
//    if (self) {
//        // Custom initialization
//    }
//    return self;
//}

- (void)viewDidUnload
{
    [super viewDidUnload];
    self.detailItem = nil;
    self.toolbar = nil;
    self.detailTitle = nil;
    self.detailDescriptionLabel = nil;
}

- (void)dealloc
{
    [_detailTitle release];
    [_detailItem release];
    [_toolbar release];
    [_detailDescriptionLabel release];
    [_contentView release];
 //   [_PanGesture release];
    [_lblRowIndex release];
    [_lblRow release];
    [_btColor release];
    [_mainNavigationBar release];
    [_tbSummary release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)modalControler:(UIViewController*)controller
{
    NSLog(@"modalControler");
    [controller.view setFrame:CGRectMake(0, -1024, 724, 1024)];
    [self.view addSubview:controller.view];
    [UIView animateWithDuration:0.6 animations:^{
        
        [controller.view setFrame:CGRectMake(0, 0, 724, 1024)];
    }];
}

-(void)dismissControler:(UIViewController*)controller
{
     NSLog(@"dismissControler");
    [UIView animateWithDuration:0.6 animations:^{
        
        [controller.view setFrame:CGRectMake(0, -1024, 724, 1024)];
    }completion:^(BOOL finished) {
        [controller.view removeFromSuperview];
    }];
}

-(void)menuHide
{
    [UIView animateWithDuration:0.6 animations:^{
        
        [self.menuController.view setFrame:CGRectMake(-250, 44, 250, 1024)];
    }];
    
    isMenuShown = NO;
}

-(void)menuShow
{
    [UIView animateWithDuration:0.6 animations:^{
        
        [self.menuController.view setFrame:CGRectMake(0, 44, 250, 1024)];
        
    }];
    
    isMenuShown = YES;
}

- (IBAction)click:(id)sender {
    [self menuShow];
}
- (IBAction)btMenuClick:(id)sender {
         [self menuShow];
   // NSLog(@"menu click");
}
- (IBAction)hidMenu:(id)sender {
    [self menuHide];
     //NSLog(@"hidMenu click");
}
- (void)addMasterButton
{
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"MENU"
                                                                      style:UIBarButtonItemStyleBordered
                                                                     target:self
                                                                     action:@selector(showMenu)];
    NSMutableArray *items = [[self.toolbar items] mutableCopy];
    [items insertObject:barButtonItem atIndex:0];
    [self.toolbar setItems:items animated:YES];
    [items release];
    [barButtonItem release];
}
- (IBAction)showMenu:(id)sender {
      [self menuShow];
    // NSLog(@"IBAction menu click");
}
- (void)showMenu
{
     [self menuShow];
    // NSLog(@"menu click");
}
- (void)hideMenu
{
    [self menuHide];
}
- (IBAction)handleTap:(UITapGestureRecognizer *)sender
{
    if (UIInterfaceOrientationIsPortrait(self.interfaceOrientation))
    {
        [self hideMenu];
        //NSLog(@"hideMenu click");

        
    }
}

//- (IBAction)handleSwipeLeft:(UISwipeGestureRecognizer *)sender
//{
//    if (UIInterfaceOrientationIsPortrait(self.interfaceOrientation))
//    {
//        [self menuHide];
//    }
//}

- (void)configureView
{
    if (self.detailItem) {
        self.detailTitle.text = [NSString stringWithFormat:@"Item %@", self.detailItem];
        self.detailDescriptionLabel.text = [NSString stringWithFormat:@"You selected item %@", self.detailItem];
    }
}
- (BOOL)shouldAutorotate
{
    //returns true if want to allow orientation change
    return TRUE;
    
    
}
- (NSUInteger)supportedInterfaceOrientations
{
    //decide number of origination tob supported by Viewcontroller.
    return UIInterfaceOrientationMaskAll;
    
    
}

//- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
//{
//    NSLog(@"hidMenu click");
//    //from here you Should try to Preferred orientation for ViewController
//}
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)  interfaceOrientation duration:(NSTimeInterval)duration
{
     

    
}



-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    
    [self portraitOrLandscape];

}
- (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer {
    
    CGPoint translation = [recognizer translationInView:self.view];

//    if (translation.x>100) {
//
//        [self menuShow];
//    
//
//    }

           if (translation.x>translation.y) {
               [self menuShow];
           }else
           {
               [self menuHide];
           }
    
    
}

-(void)portraitOrLandscape
{
    if (UIInterfaceOrientationIsPortrait(self.interfaceOrientation)) {
        [self menuHide];
        _btnMenu.hidden = NO;
        
    }
    else
    {
        [self menuShow];
        [self.menuController.view setFrame:CGRectMake(0, 44, 250, 768-44)];
        [self.contentView setFrame:CGRectMake(250, 44, 1024-250, 768-44)];
        _btnMenu.hidden = YES;
    }
}

- (IBAction)btnLogoutTouch:(id)sender {
}


- (IBAction)btnMenuTouch:(id)sender {
    if(isMenuShown == YES)
    {
        [self menuHide];
    }
    else
    {
        [self menuShow];
    }
}

-(void)menuController:(menuEGAViewController *)controller didSelectedMenuAtIndex:(NSIndexPath *)indexPath
{
        
    NSString * section = [NSString stringWithFormat:@"%d", indexPath.section];
    NSString * row = [NSString stringWithFormat:@"%d", indexPath.row];
 //   NSLog(@"ROW :%d SECTION :%d",indexPath.row,indexPath.row );
    _lblRowIndex.text=row;
    
    //if (indexPath.section == 0) 4/03/56
    if (indexPath.section == 1)
    {
        if (indexPath.row==0) {
            arcAssignEGAViewController *_arcAssignEGAViewControllerr  = [arcAssignEGAViewController alloc];
            _arcAssignEGAViewControllerr.accessibilityLabel = [section stringByAppendingString:row];
            
            [_contentView addSubview:_arcAssignEGAViewControllerr.view];
            


        }
        else
        {
        
        }
    }
    
    //if (indexPath.section == 1) 4/12/56
    if (indexPath.section == 2)
    {
        taskListAdmin *_taskListAM  = [taskListAdmin alloc];
        /*
         if (indexPath.row==0)
        {
            _taskListAM.type = @"1";
            //_taskListVC.accessibilityLabel = [section stringByAppendingString:row];
        }
        else 
         
        */
        if (indexPath.row==0)//1
        {
            _taskListAM.type = @"1";
        }
        else if (indexPath.row==1)//2
        {
            _taskListAM.type = @"2";
        }
        else if (indexPath.row==2)//3
        {
            _taskListAM.type = @"3";
        }
        [_contentView addSubview:_taskListAM.view];
    }
   // if (indexPath.section == 2) 4/12/56
    if (indexPath.section == 0)
    {
        taskListVC *_taskListVC  = [taskListVC alloc];
       /* if (indexPath.row==0)
        {
            _taskListVC.type = @"0";
            //_taskListVC.accessibilityLabel = [section stringByAppendingString:row];
        }
        else 
        */
        if (indexPath.row==0) //1
        {
            _taskListVC.type = @"1";
        }
        else if (indexPath.row==1)//2
        {
            _taskListVC.type = @"2";
        }
        else if (indexPath.row==2)//3
        {
            _taskListVC.type = @"3";
        }
        [_contentView addSubview:_taskListVC.view];
    }

    
    
    /*if (indexPath.section == 1)
    {
        taskListAdmin *_taskListAM  = [taskListAdmin alloc];
        if (indexPath.row==0)
        {
            _taskListAM.type = @"0";
            //_taskListVC.accessibilityLabel = [section stringByAppendingString:row];
        }
        else if (indexPath.row==1)
        {
            _taskListAM.type = @"1";
        }
        else if (indexPath.row==2)
        {
            _taskListAM.type = @"2";
        }
        else if (indexPath.row==3)
        {
            _taskListAM.type = @"3";
        }
        [_contentView addSubview:_taskListAM.view];
    }
    if (indexPath.section == 2)
    {
        taskListVC *_taskListVC  = [taskListVC alloc];
        if (indexPath.row==0)
        {
            _taskListVC.type = @"0";
            //_taskListVC.accessibilityLabel = [section stringByAppendingString:row];
        }
        else if (indexPath.row==1)
        {
            _taskListVC.type = @"1";
        }
        else if (indexPath.row==2)
        {
            _taskListVC.type = @"2";
        }
        else if (indexPath.row==3)
        {
            _taskListVC.type = @"3";
        }
        [_contentView addSubview:_taskListVC.view];
    }*/

//    else{
//     
//        tableViewEGAViewController *_tableViewEGAViewController  = [tableViewEGAViewController alloc];
//        _tableViewEGAViewController.accessibilityLabel = [section stringByAppendingString:row];
//        
//        [_contentView addSubview:_tableViewEGAViewController.view];
//        
//       
//        
//    }
    [self menuHide];
   // NSLog(@"menuController++++++++++++");
}


- (void)colorSelected:(NSString *)color {
    if ([color compare:@"Red"] == NSOrderedSame) {
        _lblRow.textColor = [UIColor redColor];
    } else if ([color compare:@"Green"] == NSOrderedSame) {
        _lblRow.textColor = [UIColor greenColor];
    } else if ([color compare:@"Blue"] == NSOrderedSame){
        _lblRow.textColor = [UIColor blueColor];
    }
    [self.popoverColor dismissPopoverAnimated:YES];
  //  NSLog(@"colorSelected");
}
-(UITableViewCell *)reuseTableViewCellWithIdentifier:(NSString *)identifier {
    
	//Rectangle which will be used to create labels and table view cell.
	CGRect cellRectangle;
	
	//Returns a rectangle with the coordinates and dimensions.
	cellRectangle = CGRectMake(0.0, 0.0, CELL_WIDTH, ROW_HEIGHT);
	
	//Initialize a UITableViewCell with the rectangle we created.
	UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"] autorelease];
	
	//Now we have to create the two labels.
	UILabel *label1;
	
	//Create a rectangle container for the number text.
	cellRectangle = CGRectMake(0, 0, 160,85);
	
	//Initialize the label with the rectangle.
	label1 = [[UILabel alloc] initWithFrame:cellRectangle];
	//label1.backgroundColor=[UIColor redColor];
	//Mark the label with a tag
	label1.tag = 1;
	
	//Add the label as a sub view to the cell.
	[cell.contentView addSubview:label1];
	[label1 release];
	
    UILabel *label2;
	//Create a rectangle container for the custom text.
	cellRectangle = CGRectMake(160,0,140,85);
	
	//Initialize the label with the rectangle.
	label2 = [[UILabel alloc] initWithFrame:cellRectangle];
	
	//Mark the label with a tag
    // label2.backgroundColor=[UIColor redColor];b`
	label2.tag = 2;
    label2.numberOfLines=0;
    
	//Add the label as a sub view to the cell.
	[cell.contentView addSubview:label2];
	[label2 release];
    
    
    
	return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [_summary count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        static NSString *identifier = @"MyCell";
	
	UITableViewCell *cell = [self.tbSummary dequeueReusableCellWithIdentifier:identifier];
	if(cell == nil)
		cell = [self reuseTableViewCellWithIdentifier:identifier];
	
	
	UILabel *lbl = (UILabel *)[cell viewWithTag:1];
    lbl.backgroundColor=[UIColor clearColor];
	lbl.text = [_summary objectAtIndex:indexPath.row];
//		lbl.text = @"0";
    
	UILabel *l2 = (UILabel *)[cell viewWithTag:2];
    l2.backgroundColor=[UIColor clearColor];
    l2.text= @"0      งาน";
     l2.textAlignment = UITextAlignmentCenter;
    
   // NSLog(@"%@", [_summary objectAtIndex:indexPath.row]);
    
    
       
    
    return cell;

    
    
   }

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
       
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

{
    
    UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0,5, 300, 44)] autorelease];
    UILabel *l1=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 300,44)];
  //  UILabel *l2=[[UILabel alloc]initWithFrame:CGRectMake(50,0,200,44)];
    
    //headerView.backgroundColor=[UIColor grayColor];
    l1.backgroundColor=[UIColor colorWithRed:249/255.0 green: 249/255.0 blue:249/255.0 alpha:1];
   // l2.backgroundColor=[UIColor colorWithRed:249/255.0 green: 249/255.0 blue:249/255.0 alpha:1];
        
    l1.textColor=[UIColor colorWithRed:0/255.0 green: 100/255.0 blue:0/255.0 alpha:1];
 //   l2.textColor=[UIColor colorWithRed:0/255.0 green: 100/255.0 blue:0/255.0 alpha:1];
        
    
    l1.text= @"สรุปภาพรวมสั่งการ";
   l1.textAlignment = UITextAlignmentCenter;
        //headerView.backgroundColor=[UIColor redColor];
    [headerView addSubview:l1];
  //  [headerView addSubview:l2];
    
    return headerView;
}


- (IBAction)homeTouch:(id)sender {
    
        mainEGAViewController *_arcAssignEGAViewControllerr  = [mainEGAViewController alloc];
        [self.view addSubview:_arcAssignEGAViewControllerr.view];

        // EGAMainViewController *_EGAMainViewController = [EGAMainViewController alloc];
        // [_contentView addSubview:_EGAMainViewController.view];


    
}

- (IBAction)logoutTouch:(id)sender {
    
    
    
    UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle:@"ออกจากระบบ?"
                          message:nil
                          delegate:self
                          cancelButtonTitle:@"ยกเลิก"
                          otherButtonTitles:@"ตกลง",nil] ;
    [alert show];
    [alert release];
    
    
    


}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1)
    {
        [self logout];
    }
}

-(void)logout
{
    //delete all file in sandbox document
    NSString *folderPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSError *error = nil;
    for (NSString *file in [[NSFileManager defaultManager] contentsOfDirectoryAtPath:folderPath error:&error]) {
        [[NSFileManager defaultManager] removeItemAtPath:[folderPath stringByAppendingPathComponent:file] error:&error];
    }
    
//    //back to login page
//    for (int i = 0 ; i <= 10000 ; i++){
//        [self.view removeFromSuperview];
//    }
    exit(0);
}

                          
@end
